Ext.define('Packt.controller.Login', { // #1
	extend: 'Ext.app.Controller',
	// #2
	views: ['Login', // #3
	'Header'],
	requires: ['Packt.util.MD5', 'Ext.util.Cookies', 'Packt.util.SessionMonitor', 'Packt.util.Util'],
	init: function(application) { // #4
		this.control({ // #5
			"login form button#submit": { // #1
				click: this.onButtonClickSubmit // #2
			},
			"login form button#cancel": { // #3
				click: this.onButtonClickCancel // #4
			},
			"login form textfield": {
				specialkey: this.onTextfieldSpecialKey
			},
			"appheader button#logout": {
				click: this.onButtonClickLogout
			}
		});
	},
	onTextfieldSpecialKey: function(field, e, options) {
		if (e.getKey() == e.ENTER) {
			var submitBtn = field.up('form').down('button#submit');
			submitBtn.fireEvent('click', submitBtn, e, options);
		}
	},
	onButtonClickSubmit: function(button, e, options) {
		//console.log('login submit'); // #5
		var formPanel = button.up('form'),
			login = button.up('login'),
			user = formPanel.down('textfield[name=user]').getValue(),
			pass = formPanel.down('textfield[name=password]').getValue();
		pass = Packt.util.MD5.encode(pass);
		if (formPanel.getForm().isValid()) {
			Ext.get(login.getEl()).mask("Authenticating... Please wait...", 'loading');
			Ext.Ajax.request({
				url: 'php/login.php',
				params: {
					user: user,
					password: pass
				},
				failure: function(conn, response, options, eOpts) {
					Ext.get(login.getEl()).unmask();
					Ext.Msg.show({
						title: 'Error!',
						msg: conn.responseText,
						icon: Ext.Msg.ERROR,
						buttons: Ext.Msg.OK
					});
				},
				success: function(conn, response, options, eOpts) {
					Ext.get(login.getEl()).unmask();
					var result = Ext.JSON.decode(conn.responseText, true); // #1
					if (!result) { // #2
						result = {};
						result.success = false;
						result.msg = conn.responseText;
					}
					if (result.success) { // #3
						login.close(); // #4
						Ext.util.Cookies.set("BWP", "WELCOME");
						var kunci = Ext.util.Cookies.get("BWP");
						//alert(kunci);
						Ext.create('Packt.view.MyViewport'); // #5
                        Packt.util.SessionMonitor.start();
                        //alert(kunci);
					} else {
						Ext.Msg.show({
							title: 'Fail!',
							msg: result.msg,
							// #6
							icon: Ext.Msg.ERROR,
							buttons: Ext.Msg.OK
						});
					}
				},
			});
		}
	},
	onButtonClickCancel: function(button, e, options) {
		//console.log('login cancel'); // #6
		button.up('form').getForm().reset();
	},
	onButtonClickLogout: function(button, e, options) {
		Ext.Ajax.request({
			url: 'php/logout.php',
			success: function(conn, response, options, eOpts) {
				var result =
				Packt.util.Util.decodeJSON(conn.responseText);
				if (result.success) {
					button.up('mainviewport').destroy();
					window.location.reload();
				} else {
					Packt.util.Util.showErrorMsg(conn.responseText);
				}
			},
			failure: function(conn, response, options, eOpts) {
				Packt.util.Util.showErrorMsg(conn.responseText);
			}
		});
        
        Ext.util.Cookies.clear("BWP");
	}
});